package com.yisu.expressway.onedollar.presenter.impl;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.onedollar.model.GoodsInfoObj;
import com.yisu.expressway.onedollar.model.OrderObj;
import com.yisu.expressway.onedollar.presenter.IGoodsPurchasePresenter;
import com.yisu.expressway.onedollar.view.IPurchaseView;

import android.content.Context;

/**
 * Created by shijunfeng on 2016/12/13.
 */
public class GoodsPurchasePresenterImpl implements IGoodsPurchasePresenter {
    private Context mContext;
    private IPurchaseView mView;
    private GoodsInfoObj mGoodsInfoObj;

    public GoodsPurchasePresenterImpl(Context context, IPurchaseView iPurchaseView) {
        mContext = context;
        mView = iPurchaseView;
    }

    @Override

    public void init() {

    }

    @Override
    public void fetchGoodsInfo(long goodsNo) {
        if (goodsNo <= 0) {
            return;
        }
        Map<String, Object> params = new HashMap();

        params.put("goodsno", goodsNo);
        ApiClientHelper.postApi(ApiSetting.getGoodsInfo(), new TypeToken<GoodsInfoObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<GoodsInfoObj>>() {
            @Override
            public void onResponse(ApiResult<GoodsInfoObj> result) {
                if (!result.success) {
                    mView.showToast(result.getMsg());
                    mView.refreshView(false);
                    return;
                }
                GoodsInfoObj result1 = result.getResult();
                if (result1 == null || result1.salesGoods == null){
                    mGoodsInfoObj = result1;
                    mView.refreshView(false);
                    mView.showToast("暂无该商品详情");
                }else {
                    mGoodsInfoObj = result1;
                    mView.refreshView(true);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mView.showToast(volleyError.getMessage());
                mView.refreshView(false);
            }
        }, mContext);
    }

    @Override
    public GoodsInfoObj getGoodsInfo() {
        return mGoodsInfoObj;
    }

    @Override
    public void createOrder(final String goodsName, final long barCode, final long periodsNum, final double orderAmount,
                            final long joinNum, final long goodsNo) {
        Map<String, Object> params = new HashMap();
        params.put("periodsNum", periodsNum);
        params.put("barcode", barCode);
        params.put("joinNum", joinNum);
        ApiClientHelper.postApi(ApiSetting.createOrder(), new TypeToken<OrderObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<OrderObj>>() {
            @Override
            public void onResponse(ApiResult<OrderObj> result) {
                if (!result.success) {
                    mView.showToast(result.getMsg());
                    return;
                }
                mView.jump2PayPage(goodsName, orderAmount,result.getResult(),periodsNum, joinNum,goodsNo);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mView.showToast(volleyError.getMessage());
            }
        }, mContext);
    }
}
